<?php
namespace app\admin\controller\user;

use app\admin\model\order\OrderModel;
use app\admin\model\order\OrderRecordModel;
use app\admin\model\user\User;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * Class Administrator
 * @package app\admin\controller
 * @NodeAnnotation(
 *     title="用户管理",
 *     parent="default",
 *     is_menu=1,
 *     is_view=0,
 *     auth=1,
 *     icon="/material/user.png"
 * )
 */
class Index extends Admin
{
    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new User();
    }

    /**
     * @NodeAnnotation(
     *     title="普通用户",
     *     parent="admin/user.index",
     *     is_menu=1,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/user.index"
     * )
     */
    public function index()
    {
        $keywords = $this->request->param("keywords");
        $is_admin = $this->request->param("is_admin",0,"intval");

        $where  = [];

        if($keywords) array_push($where,["fullname|username|phone|email","eq",$keywords]);

        if($is_admin || $is_admin==0) array_push($where,["is_admin","eq",$is_admin]);

        $this->assign([
            "userList" => $this->model->selectPageDatas(1,20,$where),
            "search"   => [
                "keywords" => $keywords,
                "is_admin" => $is_admin
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="创建用户",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function adduser(Request $request)
    {
        if($request->isPost())
        {
            $input = $request->post();
            if(isset($input['password'])) $input['password'] = Passwordmd5($input['password']);
            $this->add(url("admin/user.index/index"),function($model,$data){
                $model->where("id",$data['id'])->update([
                    "access_token"  => md5($data['id']),
                    "access_secret" => md5($data['id'].'199815'),
                ]);
            },$input);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑用户",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function edituser(Request $request,$id=0)
    {
        if($request->isPost())
        {
            $input = $request->post();
            if(isset($input['password'])) $input['password'] = Passwordmd5($input['password']);
            $this->edit(url("admin/user.index/index"),function($model,$data){
                if(isset($data['status']))
                {
                    \app\admin\model\user\AdminOauth::where("user_id",$data['id'])->update([
                        "status"  => $data['status'],
                    ]);
                }
            },$input);
        }

        $this->assign([
            "adminUser" => $this->model->findData($id)
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="用户详情",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function readuser(Request $request,$id=0)
    {
        $this->assign([
            "adminUser" => $this->model->findData($id)
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除用户",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function deluser(Request $request)
    {
        $this->del();
    }

    /**
     * @NodeAnnotation(
     *     title="重置用户密码",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function resetuserpwd(Request $request)
    {
        $this->edit(url("admin/user.index/index"));
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="导出excel",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function excel()
    {
        $user = $this->model->select()->toArray();
        $header = [
            "username" => "用户名",
            "phone"    => "电话",
            "create_time" => "创建时间"
        ];
        importExcel($user,$header,time());
    }

    /**
     * @NodeAnnotation(
     *     title="积分充值",
     *     parent="admin/user.index",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function score()
    {
        $price = $this->request->param("price");
        $user_id = $this->request->param("user_id");

        if($price==0|| $price<0) $this->error("请填写充值金额");

        // 1 元 = 10 积分
        $score = $price * 10;

        $data = (new OrderModel())->where("user_id",$user_id)
            ->where("price",$price)
            ->where("status",0)
            ->order("create_time desc")
            ->find();

        //$order_id = 0;

        if($data)
        {
            $res = (new OrderModel())->where("id",$data['id'])
                ->update([
                    "price" => $price,
                    "status"=> 1
                ]);
            //$order_id = $data['id'];
        }else{
            $res = (new OrderModel())->create([
                "user_id" => $user_id,
                "price"   => $price,
                "desc"    => "积分充值",
                "status"  => 1,
                "order_name"   => $score."积分",
                "order_number" => getMsecTime(). str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT)
            ]);
            //$order_id = $res['id'];
        }

        if($res)
        {
            /*if($order_id!=0)
            {
                $record = (new OrderRecordModel())->where("order_id",$order_id)->find();
                if(!$record)
                {
                    (new OrderRecordModel())->create([
                        "order_id" => $order_id,
                        "price"    => $price
                    ]);
                }
            }*/
            User::where("id",$user_id)->setInc('score',$score);
            $this->success("充值成功");
        }
        $this->error("充值失败");
    }


}